package com.ttn.util;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


public class WebUserInterceptor extends HandlerInterceptorAdapter {
	
	
	@Autowired
	private WebUserPreferences webUserPref;
	
	@Autowired
	private LanguageBean languageBean;
	
	private Set<String> blockedList;
	

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		String uri = request.getRequestURI();
		 if (blockedList.contains(uri.substring(uri.lastIndexOf("/")+1))&&
				 webUserPref.getWebUser() == null){
			 if (!uri.endsWith("/login")){
				 this.webUserPref.setRedirectUrl(uri);
			 }
			 response.sendRedirect("/ttn/"+languageBean.getLocale()+"/login");
			 return false;
		 }
		 return true;
	}


	public Set<String> getBlockedList() {
		return blockedList;
	}


	public void setBlockedList(Set<String> blockedList) {
		this.blockedList = blockedList;
	}

}
